package com.schoolgard.servlet;

import java.util.Date;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.schoolegard.services.MessageSendService;
import com.schoolegard.services.impl.MessageSendServiceImpl;
import com.supersystem.utils.DateUtil;

public class SendMessageServlet extends HttpServlet {

	private static final long serialVersionUID = -3688767350138842491L;

	public void init() throws ServletException {
		System.out.println("====告警短信监控启动====");
		Thread thread = new Thread("sendMessage"){
			public void run(){
				ServletContext sc = getServletContext(); 
				WebApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc); 
		
				MessageSendService sendService = (MessageSendServiceImpl)ac.getBean("messageSendService");
				Date initTime = new Date();
				Date netTime = DateUtil.getPlusDateOfMinute(initTime, -5);
				Date lastDate = null;
				while(true) {
					if(lastDate != null) {
						netTime = lastDate;
					}
					try {
						lastDate = sendService.send(netTime);
						Thread.sleep(5*60*1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		};
		thread.start();
	}
}
